// FACTORY.cpp : Implementation of FACTORY

#include "stdafx.h"
#include "FACTORY.h"


// FACTORY

STDMETHODIMP FACTORY::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IDMAlgorithmFactory
	};

	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}


// IDMAlgorithmFactory

STDMETHODIMP	FACTORY::CreateAlgorithm(IDMModelServices*	in_pContext,
                                                    IDMAlgorithm**		out_ppAlgorithm)
{
	// Assuming that your algorithm derives from the class DMHALLOC, we use
	// the ATL extensions in DmhMemory.h to create new instances. By deriving
	// from DMHALLOC, your algorithm can allocate memory using the model-services
	// memory allocator by simply passing a reference to itself on any new()
	// call.

	CComObjectDM<ALGORITHM>*	pAlgo;
	
	DMHALLOC	dmhalloc;

	dmhalloc.SetAllocator(in_pContext);

	HRESULT hr = CComObjectDM<ALGORITHM>::CreateInstance(dmhalloc, &pAlgo);

	if (SUCCEEDED(hr) && pAlgo)
	{
		return pAlgo->QueryInterface(__uuidof(IDMAlgorithm), (void**)out_ppAlgorithm);
	}

	return hr;
}